<template>
	<view class="custom-tab-bar">
		<view v-for="(item, index) in list" :key="index" class="tab-bar-item" :class="{ active: current === index }" @click="switchTab(index, item.pagePath)">
			<text class="tab-bar-text">{{ item.text }}</text>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		watch
	} from 'vue';
	import {
		onShow
	} from '@dcloudio/uni-app';

	const props = defineProps({
		current: {
			type: Number,
			default: 0
		}
	});

	const emit = defineEmits(['update:current']);

	const list = ref([{
			text: '商品查询',
			pagePath: '/pages/index/index',
			icon: '',
			selectedIcon: ''
		},
		{
			text: '打印编码',
			pagePath: '/pages/skuOuterIdPrint/skuOuterIdPrint',
			icon: '',
			selectedIcon: ''
		},
		{
			text: '日志',
			pagePath: '/pages/log/log',
			icon: '',
			selectedIcon: ''
		}
	]);

	// 监听页面显示，更新当前选中的tab
	onShow(() => {
		const pages = getCurrentPages();
		if (pages.length === 0) return;

		const page = pages[pages.length - 1];
		const route = page.route || page.__route__;

		const index = list.value.findIndex(item =>
			item.pagePath.includes(route)
		);

		if (index !== -1) {
			emit('update:current', index);
		}
	});

	const switchTab = (index, path) => {
		if (props.current === index) return;

		emit('update:current', index);
		uni.switchTab({
			url: path,
			success: () => {
				console.log('切换成功');
			},
			fail: (err) => {
				console.error('切换失败:', err);
			}
		});
	};
</script>

<style scoped>
	.custom-tab-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		display: flex;
		height: 50px;
		background-color: #fff;
		box-shadow: 0 -1px 6px rgba(0, 0, 0, 0.1);
		z-index: 999;
	}

	.tab-bar-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 12px;
		color: #666;
	}

	.tab-bar-item.active {
		color: #007AFF;
	}

	.tab-bar-text {
		margin-top: 4px;
		font-size: 14px;
	}

	.tab-bar-item.active .tab-bar-text {
		font-weight: bold;
		font-size: 16px;
		color: #007AFF;
	}
</style>